// 表单校验规则由 schema2code 生成，不建议直接修改校验规则，而建议通过 schema2code 生成, 详情: https://uniapp.dcloud.net.cn/uniCloud/schema


const validator = {
  "DoctorName": {
    "rules": [
      {
        "required": true,
        "errorMessage": "名字不能为空"
      },
      {
        "format": "string",
        "errorMessage": "名字不能为空"
      }
    ],
    "title": "医生名字",
    "label": "医生名字"
  },
  "doctorLevel": {
    "rules": [
      {
        "format": "string",
        "errorMessage": "请选择职称"
      },
      {
        "range": [
          {
            "text": "医生",
            "value": "医生"
          },
          {
            "text": "副主任医师",
            "value": "副主任医师"
          },
          {
            "text": "主任医师",
            "value": "主任医师"
          }
        ],
        "errorMessage": "请选择职称"
      }
    ],
    "title": "职称",
    "label": "职称"
  },
  "bean": {
    "rules": [
      {
        "required": true,
        "errorMessage": "{title}不能为空"
      },
      {
        "format": "string",
        "errorMessage": "{title}不能为空"
      },
      {
        "range": [
          {
            "text": "儿科",
            "value": "儿科"
          },
          {
            "text": "内科",
            "value": "内科"
          },
          {
            "text": "外科",
            "value": "外科"
          },
          {
            "text": "皮肤科",
            "value": "皮肤科"
          },
          {
            "text": "骨科",
            "value": "骨科"
          },
          {
            "text": "神经内科",
            "value": "神经内科"
          }
        ],
        "errorMessage": "{title}不能为空"
      }
    ],
    "title": "所属科室",
    "label": "所属科室"
  },
  "Image": {
    "rules": [
      {
        "format": "file"
      }
    ],
    "title": "医生照片",
    "label": "医生照片"
  },
  "price": {
    "rules": [
      {
        "required": true,
        "errorMessage": "{title}不能为空"
      },
      {
        "format": "int"
      },
      {
        "minimum": 1,
        "errorMessage": "{title}不能小于 {minimum}"
      }
    ],
    "title": "挂号费",
    "label": "挂号费"
  },
  "diagnosis": {
    "rules": [
      {
        "required": true,
        "errorMessage": "字符不可超过200个字符"
      },
      {
        "format": "string",
        "errorMessage": "字符不可超过200个字符"
      },
      {
        "maxLength": 200,
        "errorMessage": "字符不可超过200个字符"
      }
    ],
    "title": "主治",
    "label": "主治"
  },
  "classify": {
    "rules": [
      {
        "required": true
      },
      {
        "format": "string"
      },
      {
        "range": [
          {
            "text": "儿科",
            "value": "儿科"
          },
          {
            "text": "皮肤科",
            "value": "皮肤科"
          },
          {
            "text": "骨科",
            "value": "骨科"
          },
          {
            "text": "内科",
            "value": "内科"
          },
          {
            "text": "神经内科",
            "value": "神经内科"
          },
          {
            "text": "胃肠综合科",
            "value": "胃肠综合科"
          },
          {
            "text": "外科",
            "value": "外科"
          }
        ]
      }
    ],
    "title": "分类",
    "label": "分类"
  }
}

const enumConverter = {
  "doctorLevel_valuetotext": {
    "医生": "医生",
    "副主任医师": "副主任医师",
    "主任医师": "主任医师"
  },
  "bean_valuetotext": {
    "儿科": "儿科",
    "内科": "内科",
    "外科": "外科",
    "皮肤科": "皮肤科",
    "骨科": "骨科",
    "神经内科": "神经内科"
  },
  "classify_valuetotext": {
    "儿科": "儿科",
    "皮肤科": "皮肤科",
    "骨科": "骨科",
    "内科": "内科",
    "神经内科": "神经内科",
    "胃肠综合科": "胃肠综合科",
    "外科": "外科"
  }
}

function filterToWhere(filter, command) {
  let where = {}
  for (let field in filter) {
    let { type, value } = filter[field]
    switch (type) {
      case "search":
        if (typeof value === 'string' && value.length) {
          where[field] = new RegExp(value)
        }
        break;
      case "select":
        if (value.length) {
          let selectValue = []
          for (let s of value) {
            selectValue.push(command.eq(s))
          }
          where[field] = command.or(selectValue)
        }
        break;
      case "range":
        if (value.length) {
          let gt = value[0]
          let lt = value[1]
          where[field] = command.and([command.gte(gt), command.lte(lt)])
        }
        break;
      case "date":
        if (value.length) {
          let [s, e] = value
          let startDate = new Date(s)
          let endDate = new Date(e)
          where[field] = command.and([command.gte(startDate), command.lte(endDate)])
        }
        break;
      case "timestamp":
        if (value.length) {
          let [startDate, endDate] = value
          where[field] = command.and([command.gte(startDate), command.lte(endDate)])
        }
        break;
    }
  }
  return where
}

export { validator, enumConverter, filterToWhere }
